//
//  AddToShopCarView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/31.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "AddToShopCarView.h"
#import "JAddShopCarTopView.h"
#import "JAddShopCarNumberView.h"
//#import "JAddToShopCarRequestModel.h" // 加入购物车 或 购买 参数


@interface AddToShopCarView()

@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, strong) JAddShopCarTopView *topView;
@property (nonatomic, strong) JAddShopCarNumberView *numView;

@end

@implementation AddToShopCarView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.opaque = NO;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        _shopModel = [JAddToShopCarRequestModel new];
        _textDic = [NSDictionary dictionary];
        [self setUpView];
    }
    return  self;
}

- (void)setUpView {
    
    CGFloat numViewHeight = 180;
    _numView = [[JAddShopCarNumberView alloc] initWithFrame:CGRectMake(0, self.height - numViewHeight, kWindowW, numViewHeight)];
    [self addSubview:_numView];
    
    
    CGFloat topViewHeight = 85 + 20;
    
    _topView = [[JAddShopCarTopView alloc]initWithFrame:CGRectMake(0, self.height - numViewHeight - topViewHeight, kWindowW, topViewHeight)];
    [self addSubview:_topView];
    
    [self allBlockMethod];
    
    
}

- (void)allBlockMethod {
    __weak typeof(self) weakSelf = self;

    _numView.BtnBlock = ^{
        if (_dataArr.count == 1)
        {
            if (_shopModel.AttributesXml.length <= 0) {
                [SVProgressHUD showErrorWithStatus:@"请选择商品类型"];
                return ;
            }
        }
        else if (_dataArr.count == 2)
        {
            if (_textDic.count < 4) {
                [SVProgressHUD showErrorWithStatus:@"请选择商品类型"];
                return ;

            }
        }
        if (weakSelf.addRequestBtnBlock) {
            weakSelf.addRequestBtnBlock(weakSelf.shopModel);
        }
        
    };
    
    _numView.productNumBlock = ^(NSInteger num) {
        weakSelf.shopModel.Qty = num;
    };
    
    _topView.productAttributeBlock = ^(NSDictionary *attributeDic) {
        _textDic = attributeDic;
        [weakSelf setToShopCarDic:attributeDic];
    };
    
    _topView.closeBtnBlock = ^{
        if (weakSelf.cancleBtnBlock) {
            weakSelf.cancleBtnBlock();
        }
    };
    
    _topView.imgBtnBlock = ^(NSArray *imgArr) {
        if (weakSelf.bigImgBlock) {
            weakSelf.bigImgBlock(imgArr);
        }
    };
}

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    _topView.dataArr = dataArr;
    
    _topView.frame = CGRectMake(0, self.height - _numView.height - [_topView getViewHeight], kWindowW, [_topView getViewHeight]);
}

- (void)setProductModel:(JProductDetailModel *)productModel
{
    _productModel = productModel;
    _topView.productModel = _productModel;
    _numView.detailModel = _productModel;
    
    _shopModel.ProductId = _productModel.Id;
    _shopModel.CustomerId = [GLOBALDATA userInfoModel].CustomerId;
    _shopModel.CustomerEnteredPrice = _productModel.Price;
    _shopModel.Qty = 1;
}

- (void)setToShopCarDic:(NSDictionary *)dic
{
    
    //    "<span class="brand-color">颜色：<span class="col-brand"> 黑色 </span></span><span>尺寸：<span class="size"> 中码 </span></span>"
    
    //    "<span class="brand-color">尺寸：<span class="col-brand"> 3025-3 </span></span>"
    //    "<span class="brand-color">颜色：<span class="col-brand"> 白色6272 </span></span>"
    
    if (_dataArr.count == 0 )
    {
        
        
    }
    else if (_dataArr.count == 1 &&  dic.count == 2)
    {
        NSString *headStr1 = @"<span class=\"brand-color\">";
        NSString *headStr2 = @"<span class=\"col-brand\">";
//        NSString *headStr3 = @"</span></span><span>";
//        NSString *headStr4 = @"<span class=\"size\">";
        NSString *headStr5 = @"</span></span>";
        NSMutableString *mStr = [NSMutableString stringWithFormat:@"%@", headStr1];
        
        
        if (_dataArr.count == 1) {

            [mStr appendString:dic[@"model.product_attribute_name"]];
            [mStr appendString:@":"];
            [mStr appendString:headStr2];
            [mStr appendString:dic[@"name"]];
            [mStr appendString:headStr5];
        }
        
        
        _shopModel.AttributesXml = mStr;
    }
    
    else if (_dataArr.count == 2 &&  dic.count == 4)
    {
        NSString *headStr1 = @"<span class=\"brand-color\">";
        NSString *headStr2 = @"<span class=\"col-brand\">";
        NSString *headStr3 = @"</span></span><span>";
        NSString *headStr4 = @"<span class=\"size\">";
        NSString *headStr5 = @"</span></span>";
        NSMutableString *mStr = [NSMutableString stringWithFormat:@"%@", headStr1];
        
        [mStr appendString:dic[@"model.product_attribute_name"]];
        [mStr appendString:@":"];
        [mStr appendString:headStr2];
        [mStr appendString:dic[@"name"]];
        [mStr appendString:headStr3];
        [mStr appendString:dic[@"model.product_attribute_name2"]];
        [mStr appendString:@":"];
        [mStr appendString:headStr4];
        [mStr appendString:dic[@"name2"]];
        [mStr appendString:headStr5];
        
        _shopModel.AttributesXml = mStr;

    }
    else
    {
        [SVProgressHUD showErrorWithStatus:@"请选择商品类型"];
    }
    
}


@end
